<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-02
 * Time: 15:54
 */
namespace app\src\order\action;

use app\src\base\action\BaseAction;
use app\src\base\helper\ValidateHelper;
use app\src\goods\logic\ProductLogic;
use app\src\goods\model\Product;
use app\src\goods\model\ProductImage;
use app\src\order\logic\IntentionalOrderLogic;

class IntentionalOrderCreateAction extends BaseAction
{
    /**
     * @param $uid
     * @param $skuIds
     * @param $pIds
     * @return array|bool
     * @internal param $ids
     */
    public function create($uid,$skuIds,$pIds="",$contact_name="",$note=""){

        $logic = new IntentionalOrderLogic();
        $productLogic = new ProductLogic();

        if(empty($pIds)){
            $result = $productLogic->queryWithSkuIds($skuIds);
        }else{
            $result = $productLogic->queryWithIds($pIds);
        }

        if(!ValidateHelper::legalArrayResult($result)){
            return ['status'=>false,'info'=>lang('err_id')];
        }

        $order_items = $result['info'];
        $pid = "";
        foreach($order_items as $vo){
            $pid .= $vo['id'].',';
        }

        $result = (new ProductLogic())->queryImages(rtrim($pid,","),ProductImage::Main_Images);


        foreach($order_items as &$item){
            $item['main_img'] = 0;

            if(is_array($result['info'])){
                foreach ($result['info'] as $vo){
                    if($item['id'] == $vo['pid']){
                        $item['main_img'] = $vo['img_id'];
                    }
                }
            }
        }

        $entity= [
            'uid'=>$uid,
            'create_time'=>time(),
            'order_items'=>json_encode($order_items),
            'contact_name'=>$contact_name,
            'note'=>$note
        ];

        return $logic->add($entity);

    }
}